﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace doxyOutParser
{
	class DoxygenOutputParser
	{
		public static List<DoxygenOutputEntry> Parse(string outputFile, string filter)
		{
			List<DoxygenOutputEntry> entryList = new List<DoxygenOutputEntry>();
            try
            {
                // Using of StreamReader to parse the file
                StreamReader cycleStreamReader = new StreamReader(outputFile);
                string line = cycleStreamReader.ReadLine();
                string[] paramSeparators = { ":" };

                while (line != null)
                {
                    if (line.Length == 0)
                    {
                        line = cycleStreamReader.ReadLine();
                    }
                    else
                    {
                        string nextLine = cycleStreamReader.ReadLine();

                        if (nextLine != null)
                        {
                            if ((nextLine.StartsWith("/") && nextLine.Contains("Warning")))
                            {
                                string[] entryData = line.Split(paramSeparators, 4, StringSplitOptions.RemoveEmptyEntries);
                                DoxygenOutputEntry newEntry = new DoxygenOutputEntry(entryData);
								if (filter.Length != 0)
                                {
                                    if (newEntry.FileNameAndPath.ToString().Contains(filter))
                                    {
                                        entryList.Add(newEntry);
                                    }
                                }
                                else
                                {
                                    entryList.Add(newEntry);
                                }
                                line = nextLine;
                            }
                            else
                            {
                                line += "\r\n" + nextLine;
                            }
                        }
                        else
                        {
                            string[] entryData = line.Split(paramSeparators, 4, StringSplitOptions.RemoveEmptyEntries);
                            DoxygenOutputEntry newEntry = new DoxygenOutputEntry(entryData);
                            if (filter.Length != 0)
                            {
								if (newEntry.FileNameAndPath.ToString().Contains(filter))
                                {
                                    entryList.Add(newEntry);
                                }
                            }
                            else
                            {
                                entryList.Add(newEntry);
                            }
                            // Set line to null because nextLine has been seen null already
                            line = null;
                        }
                    }
                }
            }
            catch
            {
                throw;
            }

            return entryList;
		}
	}
}
